package org.stvd.core.web;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;

public class IOUtil {

    public static final InputStream byte2Input(byte[] buf) {
        return new ByteArrayInputStream(buf);
    }

    public static final byte[] input2byte(InputStream inStream) throws IOException {
        ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
        byte[] buff = new byte[100];
        int rc = 0;
        if (inStream.markSupported()) {
            int available = inStream.available();
            if (available <= 0) {
                inStream.reset();
            }
        }
        rc = inStream.read(buff, 0, 100);
        while (rc > 0) {
            swapStream.write(buff, 0, rc);
            rc = inStream.read(buff, 0, 100);
        }
        return swapStream.toByteArray();
    }

    public static final byte[] append(byte[] origin, byte[] newbyte) throws IOException {
        ByteArrayOutputStream swapStream = new ByteArrayOutputStream();

        swapStream.write(origin);
        swapStream.write(newbyte);

        return swapStream.toByteArray();
    }

    public static void saveFile(String filePath, String fileName, String content, boolean append) {
        mkdir(filePath);
        OutputStreamWriter writer = null;
        try {
            writer = new OutputStreamWriter(new FileOutputStream(filePath + fileName, append), "UTF-8");
            writer.write(content);
            writer.flush();
            
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (writer != null) {
                    writer.close();
                    writer = null;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            
        }
    }

    public static String readFile(String fileName) throws IOException {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), "UTF-8"));
            StringBuffer sb = new StringBuffer();
            String s = br.readLine();
            while (s != null) {
                sb.append(s).append("\r\n");
                s = br.readLine();
            }
            br.close();
            return sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    // public static String readFile(String fileName) throws IOException{
    // FileReader reader = null;
    // try{
    // reader = new FileReader(fileName);
    // BufferedReader br = new BufferedReader (reader);
    // String s;
    // StringBuffer sb = new StringBuffer();
    // while ((s=br.readLine())!=null){
    // sb.append(s+"\r\n");
    // }
    // br.close();
    // return sb.toString();
    // } catch (Exception e) {
    // e.printStackTrace();
    // } finally {
    // reader = null;
    // }
    // return "" ;
    // }

    public static boolean isFile(String filePath) {
        File fd = null;
        try {
            fd = new File(filePath);
            if (fd.exists() && fd.isFile()) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            fd = null;
        }
        return false;
    }

    @SuppressWarnings("unchecked")
    public static List getFileList(String filePath) {
        File fd = null;
        try {
            fd = new File(filePath);
            if (fd.exists() && fd.isDirectory()) {
                List list = new ArrayList();
                String[] filesName = fd.list();
                if (filesName != null) {
                    for (int i = 0; i < filesName.length; i++) {
                        List listFile = new ArrayList();
                        fd = new File(filePath + "/" + filesName[i]);
                        if (fd.isFile()) {
                            listFile.add(filesName[i]);
                            listFile.add("FILE");
                        } else {
                            listFile.add(filesName[i]);
                            listFile.add("DIR");
                        }
                        list.add(listFile);
                    }
                }
                return list;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            fd = null;
        }
        return null;
    }

    public static void mkdir(String path) {
        File fd = null;
        try {
            fd = new File(path);
            if (!fd.exists() && !fd.isDirectory()) {
                fd.mkdirs();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            fd = null;
        }

    }

    /**
     * 删除文件
     * 
     * @param srcFile
     */
    public static void deleteFile(String srcFile) {
        File file = new File(srcFile);
        if (file.exists() && file.isFile()) {
            file.delete();
        }
    }

    /**
     * 删除目录及目录下的所有文件
     * 
     * @param dir
     */
    public static void deleteDir(File dir) {
        if (dir == null || !dir.exists()) {
            return;
        }
        if (dir.isDirectory()) {
            File[] list = dir.listFiles();
            if (list != null) {
                for (File file : list) {
                    if (file.isFile())
                        file.delete();
                    else if (file.isDirectory())
                        deleteDir(file);
                }
            }
        }
        dir.delete();
    }

    public static boolean move(File srcFile, String destPath) {
        // Destination directory
        File dir = new File(destPath);

        // Move file to new directory
        return srcFile.renameTo(new File(dir, srcFile.getName()));
    }

    public static boolean move(String srcFile, String destPath) {
        // File (or directory) to be moved
        File file = new File(srcFile);

        // Destination directory
        File dir = new File(destPath);

        // Move file to new directory
        return file.renameTo(new File(dir, file.getName()));
    }

    public static void copy(String oldPath, String newPath) {
        File file = new File(oldPath);
        if (file != null && file.exists()) {
            copy(file, newPath);
        }
    }

    public static void copy(File oldfile, String newPath) {
        InputStream inStream = null;
        FileOutputStream fs = null;
        try {
            int byteread = 0;
            if (oldfile.exists()) {
                inStream = new FileInputStream(oldfile);
                fs = new FileOutputStream(newPath);
                byte[] buffer = new byte[1444];
                byteread = inStream.read(buffer);
                while (byteread != -1) {
                    fs.write(buffer, 0, byteread);
                    byteread = inStream.read(buffer);
                }
                fs.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (inStream != null) {
                    inStream.close();
                    inStream = null;
                }
            } catch (IOException e) { }
            try {
                if (fs != null) {
                    fs.close();
                    fs = null;
                }
            } catch (IOException e) { }
        }
    }
}
